﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Transactions;
using System.Windows.Forms;
using PayexpDAL;
using Utilities;
using System.Globalization;
using ExcelHandler;
using PayexpUserControls;

namespace Moneymatters
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            //TransactionTypeTreeViewForm formmm = new TransactionTypeTreeViewForm(false, true, true);
            //formmm.CreateTreeView(new List<string>());
            //formmm.ShowDialog();
            //List<TransactionType> tempp = formmm.GetCheckedLeafNodes();
            
            LoginForm loginForm = new LoginForm();
            DialogResult dr = loginForm.ShowDialog();

            //DialogResult dr = System.Windows.Forms.DialogResult.OK;

            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                InitializeComponent();
                this.Shown += Form1_Shown;
                this.tabControl.Selected += tabControl_Selected;
                this.dataGridViewTransactions.Click += dataGridViewTransactions_Click;
                this.dataGridViewTransactions.CellContentClick += dataGridViewTransactions_CellContentClick;
                this.dataGridViewTransactions.CellValueChanged += dataGridViewTransactions_CellValueChanged;

                this.dataGridBankAccounts.Click += dataGridBankAccounts_Click;

                this.dataGridViewPettyCash.CellClick += dataGridViewPettyCash_CellClick;

                this.dataGridViewHistory.Click += dataGridViewHistory_Click;

                this.dataGridViewViewBankStatements.Click += dataGridViewViewBankStatements_Click;
                this.dataGridViewViewBankStatements.CellContentClick += dataGridViewViewBankStatements_CellContentClick;

                this.filterControlReconcile.ReconciliationMode = true;

                this.vNavPaneAddMasterData.ExpandedChanged += vNavPaneAddMasterData_ExpandedChanged;
                this.vNavPaneDefaultValueSelectionTreeViews.ExpandedChanged += vNavPaneDefaultValueSelectionTreeViews_ExpandedChanged;
                
                LoadStaticLists();
                LoadBankAccounts();
                CreateRequiredFolderLocations();

                string loggedInUser = LoginForm.LoggedInUserName;
                char[] chars = loggedInUser.ToCharArray();
                string firstLetter = new string(chars, 0, 1);
                firstLetter = firstLetter.ToUpper();

                string remaining = new string(chars, 1, chars.Count() - 1);

                this.Text = string.Format("{0}  [{1}{2}]", this.Text, firstLetter, remaining);

                // set filter group boxes as disabled
                //this.groupBoxDateFilters.Enabled = false;
                //this.groupBoxAmountRangeFilters.Enabled = false;
                //this.groupBoxChequeNumberRangeFilters.Enabled = false;
                //this.groupBoxHeadDetailsFilters.Enabled = false;
                //this.groupBoxNarrationFilters.Enabled = false;
            }
            else
            {
                this.Close();
            }
        }

       

        private void Form1_Shown(object sender, EventArgs e)
        {
            this.buttonAddRowMain.PerformClick();
            this.AcceptButton = this.buttonSaveTransactionEntries;
            UpdatePettyCashLimit();

            this.labelHistoryViewTransactionMasterId.Text = string.Empty;
            this.labelHistoryViewTransactionHead.Text = string.Empty;
            this.labelHistoryViewTransactionMode.Text = string.Empty;
            this.labelHistoryViewVoucherReceiptNumber.Text = string.Empty;

            if (LoginForm.LoggedInUserName.ToLower().Equals("loginbypassed"))
            {
                return;
            }

            if (!LoginForm.LoggedInUser.IsAdmin)
            {
                this.tabControl.TabPages.Remove(this.tabPageViewHistory);
                this.tabControl.TabPages.Remove(this.tabPageReconcileTransactions);
                this.tabControl.TabPages.Remove(this.tabPageAddMasterData);

                // bank accounts tab
                this.labelBankAccountsTabInstructionLabel.Visible = false;
                this.buttonExportBankStatement.Visible = false;
                this.buttonClearBankDetailsFields.Visible = false;
                this.groupBoxAddBankDetails.Visible = false;

                // search/ export/ print tab
                this.tabPageSearchExportPrint.Text = "Search/ Print";
                this.buttonExportBankStatementsView.Visible = false;
                this.buttonViewHistoryBankStatementsView.Visible = false;
            }
        }

       
    }
}
